<?php
namespace App\Job\Prize;

use App\Query\ConfigQuery;
use App\Repository\LevelRepository;
use App\Repository\UserRepository;

// 销售奖
class Share
{
    public $money;
    
    public function __construct($money, $config, $repeat)
    {
        $this->money = $money;
        
        // 判断是否复投
        if($repeat)
        {
            // 奖励缩小
            $this->money = ConfigQuery::bi($config, 'order_repeat', $this->money);
        }
    }
    
    // 运行
    public function run($user, $content)
    {
        // 发放一级奖励
        $user = $this->prize($user, 'share', $content);
        
        // 发放二级奖励
        $this->prize($user, 'shareTwo', $content);
    }
    
    // 发放奖励
    public function prize($user, $func, $content)
    {
        // 查找上级用户
        if($user = $user->user)
        {
            // 用户
            $user = UserRepository::make($user);
            
            // 用户等级
            $level = LevelRepository::make($user->level);
            
            // 算出奖
            $money = $level->$func($this->money);
            
            // 发放
            $user->orderMoneyRemain('share', $money, $content);
            
            // 感恩奖，从关系链中查找
            (new Thank($money, $user->user_all))->run('【' . $user->username . '】感恩奖');
            
            // 返回用户
            return $user;
        }
    }
}